package base.jdk8_demo.lambda.demo08;


import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.stream.LongStream;

/**
 * jdk8 中提供了比Fork/Join更好用的，提升处理大数据的API接口。
 * 就是通过Stream的串行流进行改造成并行流，提升数据处理效率。
 * jdk8 中 Stream API可以声明性地通过 parallel() 与 sequential() 在并行流和顺序流之间进行切换。
 */
public class ParallelDemo01 {
    @Test
    public void testParallel1(){
        Instant start = Instant.now();

        LongStream longStream = LongStream.rangeClosed(0, 100L);
        LongStream parallel = longStream.parallel();
        long reduce = parallel.reduce(0, Long::sum);
        System.out.println(reduce);
        Instant end = Instant.now();
        System.out.println("计算耗时："+ Duration.between(start, end).toMillis()+" ms");
    }
    
}
